package lancooPaperRateSelf;

import android.content.Context;
import android.support.annotation.IdRes;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.presenter.PaperDef;

import org.apache.commons.lang3.StringEscapeUtils;

import lancooControl.Control;
import lancooControl.CustomEditText;

import static testPaperCTCS.GuessTestingPaperActivity.InforGuessTesting;
import static testPaperJBFX.BasicReviewPaperActivity.InforBasicReview;
import static testPaperNDFX.DifficultReviewPaperActivity.InforDifficultReview;
import static testPaperWrong.WrongQuesPaperActivity.InforWrongQues;


/**
 * Created by Administrator on 2017/8/21/021.
 */

public class RateTljdAdapter extends BaseAdapter {


    private Context mContext;
    private LayoutInflater mInflater;
    private int ItemSum;
    private int PaperID;
    private int RateID;
    private int ModelFrom;//试卷来源

    private String answer = "";//正确答案
    private String strStuAssess = "";//自评/机评结果
    private double score;//题目分值

    private ViewHolder holder = null;
    public final class ViewHolder {
        TextView TvAnswer;
        RadioGroup Rg;
        RadioButton RbA;
        RadioButton RbB;
        RadioButton RbC;
        CustomEditText EdtScore;

        private ViewHolder(View convertView) {
            TvAnswer = (TextView) convertView.findViewById(R.id.tv_answer);
            Rg = (RadioGroup) convertView.findViewById(R.id.rg);
            RbA = (RadioButton) convertView.findViewById(R.id.rb_a);
            RbB = (RadioButton) convertView.findViewById(R.id.rb_b);
            RbC = (RadioButton) convertView.findViewById(R.id.rb_c);
            EdtScore = (CustomEditText) convertView.findViewById(R.id.edt_score);
        }

    }

    public RateTljdAdapter(Context context, int itemSum, int paperID,int rateID, int modelFrom) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.ItemSum = itemSum;
        this.PaperID = paperID;
        this.RateID = rateID;
        this.ModelFrom = modelFrom;
    }

    @Override
    public int getCount() {
        return ItemSum;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_rate_tljd_item, null);
            holder = new ViewHolder(convertView);

            if(ModelFrom == PaperDef.FROM_BASE_REVIEW) {
                answer = InforBasicReview.getPaperContent().get(PaperID).getQueList().get(RateID).getAnswer();
            }else if(ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW){
                answer = InforDifficultReview.getQueList().get(RateID).getAnswer();
            }else if(ModelFrom==PaperDef.FROM_GUESS_REVIEW){
                answer = InforGuessTesting.getPaperContent().get(PaperID).getQueList().get(RateID).getAnswer();
                score = InforGuessTesting.getPaperContent().get(PaperID).getScore();
            }else if(ModelFrom==PaperDef.FROM_BASE_REVIEW_WRONG||ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW_WRONG){
                answer = InforWrongQues.get(PaperID).getQueList().get(RateID).getAnswer();
            }
            answer = StringEscapeUtils.unescapeXml(answer);
            holder.TvAnswer.setText(Html.fromHtml(answer));


            if(ModelFrom==PaperDef.FROM_GUESS_REVIEW){

                holder.Rg.setVisibility(View.GONE);
                holder.EdtScore.setVisibility(View.VISIBLE);

                holder.EdtScore.setText(GetStuAssess(RateID));
                holder.EdtScore.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        SetStuAssess(s.toString(),RateID);
                    }
                });

            }else {

                holder.Rg.setVisibility(View.VISIBLE);
                holder.EdtScore.setVisibility(View.GONE);

                setRadioGroup(RateID);
                holder.Rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                        if (checkedId == holder.RbA.getId()) {
                            SetStuAssess("优秀",RateID);
                        } else if (checkedId == holder.RbB.getId()) {
                            SetStuAssess("一般",RateID);
                        } else if (checkedId == holder.RbC.getId()) {
                            SetStuAssess("较差",RateID);
                        }
                    }
                });

            }


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

    /*
     * 设置自评
     * */
    private void setRadioGroup(int position){

        if(GetStuAssess(position).equals("优秀")){
            holder.RbA.setChecked(true);
            holder.RbB.setChecked(false);
            holder.RbC.setChecked(false);
        }else if(GetStuAssess(position).equals("一般")){
            holder.RbA.setChecked(false);
            holder.RbB.setChecked(true);
            holder.RbC.setChecked(false);
        }else if(GetStuAssess(position).equals("较差")){
            holder.RbA.setChecked(false);
            holder.RbB.setChecked(false);
            holder.RbC.setChecked(true);
        }else if(GetStuAssess(position).equals("")){
            holder.RbA.setChecked(false);
            holder.RbB.setChecked(false);
            holder.RbC.setChecked(false);
        }

    }

    /*
    * 获取当前自评结果
    * */
    private String GetStuAssess(int position){
        String strStuAssess = "";
        if(ModelFrom == PaperDef.FROM_BASE_REVIEW) {
            strStuAssess = InforBasicReview.getPaperContent().get(PaperID).getQueList().get(position).getStuAssess();
        }else if(ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW){
            strStuAssess = InforDifficultReview.getQueList().get(position).getStuAssess();
        }else if(ModelFrom==PaperDef.FROM_GUESS_REVIEW){
            strStuAssess = InforGuessTesting.getPaperContent().get(PaperID).getQueList().get(position).getStuAssess();
        }else if(ModelFrom==PaperDef.FROM_BASE_REVIEW_WRONG||ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW_WRONG){
            strStuAssess = InforWrongQues.get(PaperID).getQueList().get(position).getStuAssess();
        }
        return strStuAssess;
    }

    /*
    * 写入当前自评结果
    * */
    private void SetStuAssess(String strStuAssess,int position){

        if(ModelFrom == PaperDef.FROM_BASE_REVIEW) {
            InforBasicReview.getPaperContent().get(PaperID).getQueList().get(position).setStuAssess(strStuAssess);

            int totalItemCount = InforBasicReview.getPaperContent().get(PaperID).getTotalItemCount();
            String stuAnswerAll = "";
            String stuAssessAll = "";
            int totalItemCountTrue = 0;
            String thisStuAnswer = "";
            String thisAnswer = "";
            String thisStuAssess = "";
            int quesAnsweredItemCount = 0;
            for(int i=0;i<totalItemCount;i++){
                if(i==0){
                    stuAnswerAll = InforBasicReview.getPaperContent().get(PaperID).getQueList().get(i).getStuAnswer();
                    stuAssessAll = InforBasicReview.getPaperContent().get(PaperID).getQueList().get(i).getStuAssess();
                }else{
                    stuAnswerAll = stuAnswerAll+"#$*#"+InforBasicReview.getPaperContent().get(PaperID).getQueList().get(i).getStuAnswer();
                    stuAssessAll = stuAssessAll+"#$*#"+InforBasicReview.getPaperContent().get(PaperID).getQueList().get(i).getStuAssess();
                }
                thisStuAnswer = InforBasicReview.getPaperContent().get(PaperID).getQueList().get(i).getStuAnswer();
                thisAnswer = InforBasicReview.getPaperContent().get(PaperID).getQueList().get(i).getAnswer();
                thisStuAssess = InforBasicReview.getPaperContent().get(PaperID).getQueList().get(i).getStuAssess();
                if(!thisStuAssess.equals("")) {
                    if (thisStuAssess.equals("优秀")|| thisStuAssess.equals("一般")) {
                        totalItemCountTrue++;
                    }
                }else {
                    if (thisStuAnswer.equals(thisAnswer)) {
                        totalItemCountTrue++;
                    }
                }

                if(!thisStuAnswer.equals("")||!thisStuAssess.equals("")){
                    quesAnsweredItemCount++;
                }

            }

            InforBasicReview.getPaperContent().get(PaperID).setStuAnswerNewAll(stuAnswerAll);
            InforBasicReview.getPaperContent().get(PaperID).setStuAssessNewAll(stuAssessAll);
            InforBasicReview.getPaperContent().get(PaperID).setCorrectRate(Control.getCorrectRate(Float.valueOf(totalItemCountTrue),Float.valueOf(totalItemCount)));
            InforBasicReview.getPaperContent().get(PaperID).setAnsweredItemCount(quesAnsweredItemCount);
        }else if(ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW){
            InforDifficultReview.getQueList().get(position).setStuAssess(strStuAssess);

            int totalItemCount = InforDifficultReview.getTotalItemCount();
            String stuAnswerAll = "";
            String stuAssessAll = "";
            int totalItemCountTrue = 0;
            String thisStuAnswer = "";
            String thisAnswer = "";
            String thisStuAssess = "";
            int quesAnsweredItemCount = 0;
            for(int i=0;i<totalItemCount;i++){
                if(i==0){
                    stuAnswerAll = InforDifficultReview.getQueList().get(i).getStuAnswer();
                    stuAssessAll = InforDifficultReview.getQueList().get(i).getStuAssess();
                }else{
                    stuAnswerAll = stuAnswerAll+"#$*#"+InforDifficultReview.getQueList().get(i).getStuAnswer();
                    stuAssessAll = stuAssessAll+"#$*#"+InforDifficultReview.getQueList().get(i).getStuAssess();
                }
                thisStuAnswer = InforDifficultReview.getQueList().get(i).getStuAnswer();
                thisAnswer = InforDifficultReview.getQueList().get(i).getAnswer();
                thisStuAssess = InforDifficultReview.getQueList().get(i).getStuAssess();
                if(!thisStuAssess.equals("")) {
                    if (thisStuAssess.equals("优秀")|| thisStuAssess.equals("一般")) {
                        totalItemCountTrue++;
                    }
                }else {
                    if (thisStuAnswer.equals(thisAnswer)) {
                        totalItemCountTrue++;
                    }
                }

                if(!thisStuAnswer.equals("")||!thisStuAssess.equals("")){
                    quesAnsweredItemCount++;
                }

            }

            InforDifficultReview.setStuAnswerNewAll(stuAnswerAll);
            InforDifficultReview.setStuAssessNewAll(stuAssessAll);
            InforDifficultReview.setCorrectRate(Control.getCorrectRate(Float.valueOf(totalItemCountTrue),Float.valueOf(totalItemCount)));
            InforDifficultReview.setAnsweredItemCount(quesAnsweredItemCount);
        }else if(ModelFrom==PaperDef.FROM_GUESS_REVIEW){
            InforGuessTesting.getPaperContent().get(PaperID).getQueList().get(position).setStuAssess(strStuAssess);

            int totalItemCount = InforGuessTesting.getPaperContent().get(PaperID).getTotalItemCount();
            String stuAnswerAll = "";
            String stuAssessAll = "";
            int totalScore = 0;
            String thisStuAnswer = "";
            String thisAnswer = "";
            String thisStuAssess = "";
            int quesAnsweredItemCount = 0;
            for(int i=0;i<totalItemCount;i++){
                if(i==0){
                    stuAnswerAll = InforGuessTesting.getPaperContent().get(PaperID).getQueList().get(i).getStuAnswer();
                    stuAssessAll = InforGuessTesting.getPaperContent().get(PaperID).getQueList().get(i).getStuAssess();
                }else{
                    stuAnswerAll = stuAnswerAll+"#$*#"+InforGuessTesting.getPaperContent().get(PaperID).getQueList().get(i).getStuAnswer();
                    stuAssessAll = stuAssessAll+"#$*#"+InforGuessTesting.getPaperContent().get(PaperID).getQueList().get(i).getStuAssess();
                }
                thisStuAnswer = InforGuessTesting.getPaperContent().get(PaperID).getQueList().get(i).getStuAnswer();
                thisAnswer = InforGuessTesting.getPaperContent().get(PaperID).getQueList().get(i).getAnswer();
                thisStuAssess = InforGuessTesting.getPaperContent().get(PaperID).getQueList().get(i).getStuAssess();
                if(!thisStuAssess.equals("")) {
                    totalScore = totalScore+Integer.valueOf(thisStuAssess);
                }else {
                    if (thisStuAnswer.equals(thisAnswer)) {
                        totalScore = totalScore+(int) score/totalItemCount;
                    }
                }

                if(!thisStuAnswer.equals("")||!thisStuAssess.equals("")){
                    quesAnsweredItemCount++;
                }

            }

            InforGuessTesting.getPaperContent().get(PaperID).setStuAnswerNewAll(stuAnswerAll);
            InforGuessTesting.getPaperContent().get(PaperID).setStuAssessNewAll(stuAssessAll);
            InforGuessTesting.getPaperContent().get(PaperID).setCorrectRate(Control.getCorrectRate(Float.valueOf(totalScore), (float) score));
            InforGuessTesting.getPaperContent().get(PaperID).setAnsweredItemCount(quesAnsweredItemCount);
        }else if(ModelFrom==PaperDef.FROM_BASE_REVIEW_WRONG||ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW_WRONG){
            InforWrongQues.get(PaperID).getQueList().get(position).setStuAssess(strStuAssess);

            int totalItemCount = InforWrongQues.get(PaperID).getTotalItemCount();
            String stuAnswerAll = "";
            String stuAssessAll = "";
            int totalItemCountTrue = 0;
            String thisStuAnswer = "";
            String thisAnswer = "";
            String thisStuAssess = "";
            int quesAnsweredItemCount = 0;
            for(int i=0;i<totalItemCount;i++){
                if(i==0){
                    stuAnswerAll = InforWrongQues.get(PaperID).getQueList().get(i).getStuAnswer();
                    stuAssessAll = InforWrongQues.get(PaperID).getQueList().get(i).getStuAssess();
                }else{
                    stuAnswerAll = stuAnswerAll+"#$*#"+InforWrongQues.get(PaperID).getQueList().get(i).getStuAnswer();
                    stuAssessAll = stuAssessAll+"#$*#"+InforWrongQues.get(PaperID).getQueList().get(i).getStuAssess();
                }
                thisStuAnswer = InforWrongQues.get(PaperID).getQueList().get(i).getStuAnswer();
                thisAnswer = InforWrongQues.get(PaperID).getQueList().get(i).getAnswer();
                thisStuAssess = InforWrongQues.get(PaperID).getQueList().get(i).getStuAssess();
                if(!thisStuAssess.equals("")) {
                    if (thisStuAssess.equals("优秀")|| thisStuAssess.equals("一般")) {
                        totalItemCountTrue++;
                    }
                }else {
                    if (thisStuAnswer.equals(thisAnswer)) {
                        totalItemCountTrue++;
                    }
                }

                if(!thisStuAnswer.equals("")||!thisStuAssess.equals("")){
                    quesAnsweredItemCount++;
                }

            }

            InforWrongQues.get(PaperID).setStuAnswerNewAll(stuAnswerAll);
            InforWrongQues.get(PaperID).setStuAssessNewAll(stuAssessAll);
            InforWrongQues.get(PaperID).setCorrectRate(Control.getCorrectRate(Float.valueOf(totalItemCountTrue),Float.valueOf(totalItemCount)));
            InforWrongQues.get(PaperID).setAnsweredItemCount(quesAnsweredItemCount);
        }

        if(ModelFrom!=PaperDef.FROM_GUESS_REVIEW) {
            setRadioGroup(position);
        }
    }

}